#include "cadrefamilytab.h"
#include "ui_cadrefamilytab.h"
#include <QDialog>
#include <QGridLayout>
#include <QLineEdit>
#include <QSqlQuery>
#include <QPushButton>
#include <QSqlTableModel>
#include "timeeditdelegate.h"
#include <QDebug>
#include <QSqlRelationalTableModel>
#include <QSqlRelationalDelegate>
#include <QLabel>
#include <QHash>
#include <QDialogButtonBox>
#include <QMessageBox>
#include <QSqlError>
#include "cdateedit.h"
#include "cadreutil.h"

CadreFamilyTab::CadreFamilyTab(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CadreFamilyTab)
{
    ui->setupUi(this);
}

CadreFamilyTab::~CadreFamilyTab()
{
    delete ui;
}
void CadreFamilyTab::initFamilyTableView(QString curPerson)
{
    familyModel = new QSqlRelationalTableModel(this);
    familyModel->setTable("Family");
    familyModel->setRelation(3, QSqlRelation("RelationCode", "RelationID", "Relation"));
    familyModel->setRelation(4, QSqlRelation("PoliticCode", "PoliticID", "Politic"));
    familyModel->setRelation(10, QSqlRelation("FamilyStatusCode", "FamilyStatusID", "FamilyStatus"));
    familyModel->setJoinMode(QSqlRelationalTableModel::LeftJoin);
    familyModel->setEditStrategy(QSqlTableModel::OnFieldChange);// OnRowChange OnFieldChange OnManualSubmit
    QString filter = QString::fromUtf8("IDNum='%1'").arg(curPerson);
    currentPerson=curPerson;
    familyModel->setFilter(filter);
    familyModel->select();
    familyModel->setHeaderData(1, Qt::Horizontal, QString::fromUtf8("姓名"));
    familyModel->setHeaderData(2, Qt::Horizontal, QString::fromUtf8("出生日期"));
    familyModel->setHeaderData(3, Qt::Horizontal, QString::fromUtf8("关系"));
    familyModel->setHeaderData(4, Qt::Horizontal, QString::fromUtf8("政治面貌"));
    familyModel->setHeaderData(5, Qt::Horizontal, QString::fromUtf8("工作单位"));
    familyModel->setHeaderData(6, Qt::Horizontal, QString::fromUtf8("职业"));
    familyModel->setHeaderData(8, Qt::Horizontal, QString::fromUtf8("证件号码"));
    familyModel->setHeaderData(9, Qt::Horizontal, QString::fromUtf8("备注"));
    familyModel->setHeaderData(10, Qt::Horizontal, QString::fromUtf8("状态"));
    ui->familyTableView->horizontalHeader()->setVisible(true); //UI界面设置，总是恢复为false
    ui->familyTableView->setModel(familyModel);
    ui->familyTableView->setItemDelegate(new QSqlRelationalDelegate(ui->familyTableView));
    TimeEditDelegate *birthtime = new TimeEditDelegate();
    ui->familyTableView->setItemDelegateForColumn(2,birthtime);
    ui->familyTableView->setColumnHidden(0,true);
    ui->familyTableView->setColumnHidden(7,true);
    ui->familyTableView->setColumnWidth(1,90);
    ui->familyTableView->setColumnWidth(2,130);
    ui->familyTableView->setColumnWidth(3,80);
    ui->familyTableView->setColumnWidth(4,100);
    ui->familyTableView->setColumnWidth(5,150);
    ui->familyTableView->setColumnWidth(6,130);
    ui->familyTableView->setColumnWidth(8,150);
    ui->familyTableView->setColumnWidth(9,100);
    ui->familyTableView->setColumnWidth(10,100);
    ui->familyTableView->selectRow(familyModel->rowCount()-1);
}
void CadreFamilyTab::setButtonEnable(bool i)
{
    ui->familyAddButton->setEnabled(i);
    ui->familyDeleteButton->setEnabled(i);
    ui->familyDownButton->setEnabled(i);
    ui->familyUpButton->setEnabled(i);
    if(i){
        ui->familyTableView->setEditTriggers(QAbstractItemView::DoubleClicked);
    }else{
        ui->familyTableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    }

}

void CadreFamilyTab::on_familyAddButton_clicked()
{
    QDialog *addDialog = new QDialog(this);
    addDialog->setWindowTitle(trUtf8("添加家庭成员"));
    addDialog->setGeometry(700,300,900,480);
    addDialog->setContentsMargins(10,0,10,0);
    QGridLayout *gridLayout = new QGridLayout(addDialog);
    gridLayout->setMargin(10);
    QLabel *nameLabel =new QLabel(trUtf8("姓名"),addDialog);
    QLabel *birthLabel=new QLabel(trUtf8("出生日期"),addDialog);
    QLabel *relationLabel=new QLabel(trUtf8("关系"),addDialog);
    QLabel *politicLabel=new QLabel(trUtf8("政治面貌"),addDialog);
    QLabel *unitLabel=new QLabel(trUtf8("工作单位"),addDialog);
    QLabel *professionLabel=new QLabel(trUtf8("职业"),addDialog);
    QLabel *familyNumLabel=new QLabel(trUtf8("证件号码"),addDialog);
    QLabel *remarkLabel=new QLabel(trUtf8("备注"),addDialog);
    QLabel *statusLabel=new QLabel(trUtf8("状态"),addDialog);
    QLineEdit *name = new QLineEdit(addDialog);
    CDateEdit *birthtime = new CDateEdit(addDialog);
    birthtime->setNullable(true);
    birthtime->setDisplayFormat("yyyy-MM-dd");
    birthtime->setCalendarPopup(true);
    birthtime->setDate(QDate());
    QComboBox *politic = new QComboBox(addDialog);
    phash = CadreUtil::initComboBox(politic,"","PoliticCode","PoliticID","Politic");
    QComboBox *relation = new QComboBox(addDialog);
    rhash =CadreUtil::initComboBox(relation,"","RelationCode","RelationID","Relation");
    QLineEdit *unit = new QLineEdit(addDialog);
    QLineEdit *profession = new QLineEdit(addDialog);
    QLineEdit *familynum = new QLineEdit(addDialog);
    QLineEdit *remark = new QLineEdit(addDialog);
    QComboBox *status = new QComboBox(addDialog);
    statushash =CadreUtil::initComboBox(status,"","FamilyStatusCode","FamilyStatusID","FamilyStatus");
    gridLayout->addWidget(nameLabel,0,0);
    gridLayout->addWidget(name,0,1);
    gridLayout->addWidget(birthLabel,0,2);
    gridLayout->addWidget(birthtime,0,3);
    gridLayout->addWidget(relationLabel,1,0);
    gridLayout->addWidget(relation,1,1);
    gridLayout->addWidget(politicLabel,1,2);
    gridLayout->addWidget(politic,1,3);
    gridLayout->addWidget(unitLabel,2,0);
    gridLayout->addWidget(unit,2,1);
    gridLayout->addWidget(professionLabel,2,2);
    gridLayout->addWidget(profession,2,3);
    gridLayout->addWidget(familyNumLabel,3,0);
    gridLayout->addWidget(familynum,3,1);
    gridLayout->addWidget(remarkLabel,3,2);
    gridLayout->addWidget(remark,3,3);
    gridLayout->addWidget(statusLabel,4,0);
    gridLayout->addWidget(status,4,1);

    QPushButton *yesButton = new QPushButton(trUtf8("确定"));
    QPushButton *cancelButton = new QPushButton(trUtf8("取消"));

    QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Horizontal);
    buttonBox->addButton(yesButton, QDialogButtonBox::AcceptRole);
    buttonBox->addButton(cancelButton, QDialogButtonBox::RejectRole);

    gridLayout->addWidget(buttonBox,5,1,1,4,Qt::AlignHCenter);

    connect(buttonBox, SIGNAL(accepted()), addDialog, SLOT(accept()));
    connect(buttonBox, SIGNAL(rejected()), addDialog, SLOT(reject()));

    if(addDialog->exec()==QDialog::Accepted){
        if(name->text().isEmpty()){
            QMessageBox::information(this,trUtf8("提示"),
               trUtf8("名字不能为空"),QMessageBox::Yes);
            return;
        }
        QSqlQuery query;
        query.prepare("insert into Family (`Name`, `BirthTime`, `Relation`, `Politic`,"
                      " `UnitName`,`Profession`, `IDNum`, `FamilyNum`, `FileRecRmk`, `FamilyStatusID`)"
                      " values(:name, :birthtime, :relation, :politic, :unitname, "
                      ":profession, :idnum, :familynum, :filerecrmk, :status )");
        query.bindValue(":name",name->text());
        query.bindValue(":birthtime",birthtime->date());
        query.bindValue(":relation",rhash.key(relation->currentText()));
        query.bindValue(":politic",phash.key(politic->currentText()));
        query.bindValue(":unitname",unit->text());
        query.bindValue(":profession",profession->text());
        query.bindValue(":familynum",familynum->text());
        query.bindValue(":filerecrmk",remark->text());
        query.bindValue(":status",statushash.key(status->currentText()));
        query.bindValue(":idnum",currentPerson);
        if(query.exec()){
            QMessageBox::information(this,trUtf8("提示"),
               trUtf8("插入数据成功"),QMessageBox::Yes);
            initFamilyTableView(currentPerson);
        }else{
            QString info = query.lastError().databaseText();
            QMessageBox::information(this,trUtf8("插入数据库错误"),
               info,QMessageBox::Yes);
        }
    }else{

    }
}


QSqlTableModel* CadreFamilyTab::getOriginalModel()
{
    QSqlTableModel *originalfamilyModel = new QSqlTableModel(this);
    originalfamilyModel->setTable("Family");
    QString filter = QString::fromUtf8("IDNum='%1'").arg(currentPerson);
    originalfamilyModel->setFilter(filter);
    originalfamilyModel->select();
    originalfamilyModel->setEditStrategy(QSqlTableModel::OnFieldChange);
    return originalfamilyModel;
}

void CadreFamilyTab::on_familyUpButton_clicked()
{
    QSqlTableModel *originalfamilyModel = getOriginalModel();
    int cur = CadreUtil::moveUpOne(originalfamilyModel,0,ui->familyTableView);
    initFamilyTableView(currentPerson);
    ui->familyTableView->selectRow(cur);
}

void CadreFamilyTab::on_familyDownButton_clicked()
{
    QSqlTableModel *originalfamilyModel = getOriginalModel();
    int cur = CadreUtil::moveDownOne(originalfamilyModel,0,ui->familyTableView);
    initFamilyTableView(currentPerson);
    ui->familyTableView->selectRow(cur);
}

void CadreFamilyTab::on_familyDeleteButton_clicked()
{
   CadreUtil::deleteRow(familyModel, ui->familyTableView);
}
